package com.tuoyu.peopletest.view.ac;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LanguageUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.lxj.xpopup.XPopup;
import com.tuoyu.peopletest.R;
import com.tuoyu.peopletest.common.MyApplication;
import com.tuoyu.peopletest.entity.message.ForegroundMessageEvent;
import com.tuoyu.peopletest.entity.message.MyMessageEvent;
import com.tuoyu.peopletest.entity.message.NetWorkMessageEvent;
import com.tuoyu.peopletest.presenter.LoadUIPresenter;
import com.tuoyu.peopletest.utils.SCLogUtils;
import com.tuoyu.peopletest.utils.SCUIUtils;
import com.tuoyu.peopletest.view.iview.IView;
import com.tuoyu.peopletest.view.pop.ResultPop;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


/**
 * activity 基类
 */
public abstract class BaseActivity extends AppCompatActivity implements IView {

    //ac的标记
    public final String TAG = this.getClass().getSimpleName();

    //基础总布局
    public ViewGroup vBaseMain;


    //toast布局
    public TextView vBaseToast;
    public TextView vBaseToastTop;

    public boolean isConnectNet;

    //页面是否可见
    public boolean isResume;
    public boolean isStart;
    //页面是否走 onStart   onStop
    public boolean isPageStart;

    public LoadUIPresenter mLoadUIPresenter;

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        LanguageUtils.attachBaseContext(getApplicationContext());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SCLogUtils.e(TAG, "onCreate");
        //BarUtils.transparentStatusBar(this);
        EventBus.getDefault().register(this);

        // 有时候,网络连接着,但是返回false 因为判断网络是否连接是在子线程做的
        isConnectNet = MyApplication.isConnectNet;

        //不要统一做, UI如果换人了, 设计稿就变了
        //SCDensityUtils.setDensity(getApplication(),this);


        initContentView();
        //加载弹窗
        mLoadUIPresenter = LoadUIPresenter.get();
        mLoadUIPresenter.setMainView(vBaseMain);
        setLoadingUI();

        initChildView();
        initData();
        initListener();
    }


    @Override
    public void setContentView(int layoutResID) {
        //加载基础总布局
        ViewGroup baseView = (ViewGroup) View.inflate(this, R.layout.acitivity_base, null);
        //子布局要填充的总布局
        vBaseMain = baseView.findViewById(R.id.v_ac_base_main);
        RelativeLayout vBaseContent = baseView.findViewById(R.id.sc_base_content);
        vBaseToast = baseView.findViewById(R.id.sc_base_toast);
        vBaseToast.setAlpha(0);
        vBaseToastTop = baseView.findViewById(R.id.sc_base_toast_top);
        vBaseToastTop.setAlpha(0);


        //子布局
        View child = View.inflate(this, layoutResID, null);
        //添加总布局
        RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        vBaseContent.addView(child, relLayoutParams);


        super.setContentView(baseView);

    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
    }


    /**
     * 设置布局
     */
    public abstract void initContentView();

    /**
     * 初始化子布局
     */
    public abstract void initChildView();

    /**
     * 设置监听
     */
    public void initData() {

    }

    /**
     * 设置监听
     */
    public void initListener() {

    }


    @Override
    protected void onStart() {
        super.onStart();
        isPageStart = true;
        SCLogUtils.e(TAG, "onStart");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        isPageStart = true;
        SCLogUtils.e(TAG, "onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        isResume = true;
        SCLogUtils.e(TAG, "onResume");
    }

    /**
     * 设置价值UI
     */
    public void setLoadingUI() {
        //mLoadUIPresenter.setType(LoadUIPresenter.NOMAL);
        //mLoadUIPresenter.initNomalUI();
    }

    @Override
    public void showLoading() {
        //mLoadUIPresenter.showLoading();
    }

    @Override
    public void hideLoading() {
        //mLoadUIPresenter.hideLoading();
    }

    @Override
    public void showToast(Object obj) {
        if (obj instanceof Integer) {
            vBaseToast.setText((Integer) obj);
        } else if (obj instanceof String) {
            vBaseToast.setText((String) obj);
        }

        vBaseToast.setScaleX(0.9f);
        vBaseToast.setScaleY(0.9f);
        vBaseToast.animate().alpha(1f).scaleX(1).scaleY(1).setDuration(200).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                vBaseToast.postDelayed(() -> {
                    vBaseToast.animate().alpha(0f).setDuration(200).setListener(null).start();
                    ;
                }, 1500);
            }
        }).start();
    }

    @Override
    public void showToastTop(Object obj) {
        if (obj instanceof Integer) {
            vBaseToastTop.setText((Integer) obj);
        } else if (obj instanceof String) {
            vBaseToastTop.setText((String) obj);
        }

        vBaseToastTop.setScaleX(0.9f);
        vBaseToastTop.setScaleY(0.9f);
        vBaseToastTop.animate().alpha(1f).scaleX(1).scaleY(1).setDuration(200).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                vBaseToastTop.postDelayed(() -> {
                    vBaseToastTop.animate().alpha(0f).setDuration(200).setListener(null).start();
                    ;
                }, 1500);
            }
        }).start();
    }

    @Override
    public void showDialogResult(Object obj) {
        ResultPop pop = new ResultPop(this, obj);
        new XPopup.Builder(this)
                .dismissOnTouchOutside(true)
                .asCustom(pop)
                .show();
    }

    @Override
    protected void onPause() {
        super.onPause();
        isResume = false;
        SCLogUtils.e(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        isPageStart = false;
        SCLogUtils.e(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        SCLogUtils.e(TAG, "onDestroy");
    }

    @Override
    public void onBackPressed() {
        SCLogUtils.e(TAG, "onBackPressed");
        if (mLoadUIPresenter != null && mLoadUIPresenter.isShowLoading()) {
            onBackCloseLoading();
            return;
        }

        if (!onBackFinishBtn()) {
            return;
        }
        super.onBackPressed();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onForegroundEvent(ForegroundMessageEvent event) {
        //前后台切换了
        if (event.isAppForeground) {
            onForeground(event.ac);
        } else {
            onBackground(event.ac);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onNetWorkEvent(NetWorkMessageEvent event) {
        //网络变化
        if (event.isNetConnected) {
            //连接

            isConnectNet = MyApplication.isConnectNet = true;
            onConnected(event.networkType);
        } else {
            //断开
            isConnectNet = MyApplication.isConnectNet = false;
            onDisconnected();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onNotifyEvent(MyMessageEvent event) {
        onNotice(event);
    }

    /**
     * 网络变化监听器
     */
    public void onDisconnected() {
        SCLogUtils.e(TAG, "网络变化不可用");
        //网络是否可用,


    }

    /**
     * 网络重新连接回调
     *
     * @param networkType
     */

    public void onConnected(NetworkUtils.NetworkType networkType) {

        SCLogUtils.e(TAG, "网络变化 可用");
    }

    /**
     * 设置返回点击相应
     *
     * @param resBack
     */
    public void setBackEvent(int resBack) {
        findViewById(resBack).setOnClickListener((v) -> {
            if (mLoadUIPresenter != null && mLoadUIPresenter.isShowLoading()) {
                onBackCloseLoading();
                return;
            }
            //关闭
            if (onBackFinishBtn()) {
                finish();
            }

        });
    }

    /**
     * 获得EventBus通知
     *
     * @param event
     */
    protected void onNotice(MyMessageEvent event) {

    }

    /**
     * app 回到前台回调
     */
    public void onForeground(Activity activity) {

    }

    /**
     * app推入后台回调
     */
    public void onBackground(Activity activity) {

    }

    /**
     * 返回关闭按钮
     */
    protected boolean onBackFinishBtn() {

        return true;
    }

    /**
     * 通过返回键关闭加载框
     */
    public void onBackCloseLoading() {

    }

    /**
     * 设置标题文字
     *
     * @param object
     */
    public void setTitle(int resId, Object object) {
        SCUIUtils.setTextStr(((TextView) findViewById(resId)), object);
    }

    /**
     * 获取空间内容
     */
    public String getTvStr(TextView tv) {
        return tv.getText().toString().trim();
    }
}
